********************************************************************************
* Prepare WIOD:
* 		merge with selected sectors / countries / periods
* 		interpolate missings
********************************************************************************

********************************************************************************
* Main settings
********************************************************************************
set more off
clear all

*global rootfolder ""
global folder "$rootfolder\analysis"
global inputfolder "$rootfolder\data"
global CSfolder "$folder\CS"

********************************************************************************
capture mkdir "$folder\WIOD"
global outputfolder "$folder\WIOD"

foreach mat in GAMMA ALPHA SIGMA SWEIGHTS TOWEIGHTS totalOutput{
	capture mkdir "$outputfolder\\`mat'"
	global `mat'folder "$outputfolder\\`mat'"
	
	capture mkdir "$outputfolder\\`mat'\MatlabFiles"
	global `mat'matlabfolder "$outputfolder\\`mat'\MatlabFiles"
}
********************************************************************************
global YEAR_START 1995
global YEAR_END 2011
********************************************************************************

********************************************************************************
* Prepare/order WIOD data
********************************************************************************
use "$inputfolder\3_WIOD\WIOD.dta", clear

* Variable Info:
* country = c_I (importing country)
* country_ind = s_I (importing sector)
* partner = c_E (exporting country)
* partner_ind = s_E (exporting sector)
* year = t
* total_output = output_{c_I,s_I}
* tot_inter_cons = inter_{c_I,s_I}
* tot_inter_use_from_ind = inter_{c_I,s_I}
* inter_use = M_{c_I,s_I,c_E,s_E} (value of intermediary goods exported from {c_E,s_E} to {c_I,s_I})

* Use the defined set of {c_I,s_I,c_E,s_E}:
joinby year country country_ind partner partner_ind using "$CSfolder\tempcscsset.dta", unm(both)

* Drop countries and sectors which are not in the defined set: (all obs should be in the set)
keep if _merge==3 | _merge==2
drop _merge
order year c_I s_I c_E s_E inter_use total_output country country_ind partner partner_ind
sort year c_I s_I c_E s_E

* MARKET SHARE:
gen market_share=inter_use/total_output
compress
save "$outputfolder\WIOD_ordered.dta", replace

********************************************************************************
* 17 sectors X 31 countries
********************************************************************************

********************************************************************************
* Generate Srest ROW
* 17 X 31
********************************************************************************
use "$outputfolder\WIOD_ordered.dta", clear
* Final sector list: 17 (no Srest)
local Li_Sectors "15t16 17t18 19 20 21t22 23 24 25 26 27t28 29 30t33 34t35 36t37 atb c e"
* Final country list: 30 + ROW
local Li_Ctrys "AUS	AUT BEL BGR CAN CHN CZE DEU DNK ESP FIN FRA GBR GRC HUN IRL ITA JPN KOR LTU MEX NLD POL PRT ROM RUS SVN SWE TWN USA"

** Create Srest and ROW 
gen tmpd_partner_row = 1
gen tmpd_ctry_row = 1
foreach b of local Li_Ctrys { 
	replace tmpd_ctry_row = 0 if country == "`b'"
	replace tmpd_partner_row = 0 if partner == "`b'"
}
gen tmpd_ctry_Srst = 1
gen tmpd_partner_Srst = 1
foreach b of local Li_Sectors { 
	replace tmpd_ctry_Srst = 0 if country_ind == "`b'"
	replace tmpd_partner_Srst = 0 if partner_ind == "`b'" 
} 
* All remaining to ROW - partnerwise
replace partner = "ROW" if tmpd_partner_row == 1
replace c_E = 999 if tmpd_partner_row == 1

* All remaining to Srest / drop Srest - partnerwise
*replace partner_ind = "Srest" if tmpd_partner_Srst == 1
*replace s_E = 999 if tmpd_partner_Srst == 1
drop if tmpd_partner_Srst == 1

keep inter_use total_output tot_inter_cons tot_val_add  tmpd_ctry_row tmpd_ctry_Srst year c_I s_I c_E s_E country country_ind partner partner_ind
collapse (sum) inter_use (firstnm) total_output tot_inter_cons tot_val_add (mean) tmpd_ctry_row tmpd_ctry_Srst, by(year c_I s_I c_E s_E country country_ind partner partner_ind)

* All remaining to ROW - countrywise
replace country = "ROW" if tmpd_ctry_row == 1
replace c_I = 999 if tmpd_ctry_row == 1

* All remaining to Srest / drop Srest - countrywise
*replace country_ind = "Srest" if tmpd_ctry_Srst == 1
*replace s_I = 999 if tmpd_ctry_Srst == 1
drop if tmpd_ctry_Srst == 1

keep inter_use total_output tot_inter_cons tot_val_add  c_I s_I c_E s_E year country country_ind partner partner_ind
collapse (sum) inter_use total_output tot_inter_cons tot_val_add (firstnm) c_I s_I c_E s_E, by(year country country_ind partner partner_ind)

tabulate country_ind partner_ind
if r(r) != r(c) {
	break
	display as error "WIOD is not symmetric w.r.t. to Sectors"
}

save "$outputfolder/reduced_WIOD.dta", replace

********************************************************************************
* GAMMA: share of expenditure on input es by cu in the value of total output_cu 
* 17 X 31
********************************************************************************	
** calculating market shares and scale gammas 
use "$outputfolder/reduced_WIOD.dta", clear
gen market_share = inter_use/ total_output
replace market_share = 0 if total_output == 0 & inter_use == 0
bysort year country country_ind: egen tot_ms = total(market_share)
gen market_share_rescal = market_share/tot_ms
bysort year country country_ind: egen rescal_tot_ms = total(market_share_rescal)

* generate Linkages in size(NM,NM) weights_cI_sI_cE_sE = Input_cE_sE / Output_cI_sI
keep year c_* s_* countr* partne* market_share market_share_rescal total_output
sort year country country_ind

* reshape to wide and save as csv for matlab use
keep year country country_ind partner partner_ind market_share
preserve

forval yy = $YEAR_START / $YEAR_END {
	drop if year != `yy'	
	sort country country_ind partner partner_ind
	

	reshape wide market_share, i(country country_ind partner year) j(partner_ind) string
	foreach var of varlist market_share* {
		rename `var' `var'_
	}
	reshape wide market_share*, i(country country_ind year) j(partner) string
	order year country country_ind market_share*
	sort country country_ind
	
	compress
	format market_share* %15.0g
	outsheet using "$GAMMAmatlabfolder\WIOD_gamma_`yy'.csv", comma replace
	restore, preserve
}
restore

********************************************************************************
* ALPHA = sales of products from cu in market rs / total sales of products from cu in all markets 
* 17 X 31
********************************************************************************
** calculating alpha_rscu = Y_rscu / sum(Y_r's'cu) *****************************
use "$outputfolder/reduced_WIOD.dta", clear

* Y_rscu: sales of products from cu in market rs
bysort year partner partner_ind: egen tot_sales = total(inter_use)
gen alpha = inter_use/ tot_sales
replace alpha = 0 if tot_sales == 0 & inter_use == 0

bysort year partner partner_ind: egen tot_sales_share = total(alpha)
tab tot_sales_share

* reshape to wide and save as csv for matlab use
keep year country country_ind partner partner_ind alpha
preserve

forval yy = $YEAR_START / $YEAR_END {
	drop if year != `yy'	
	sort country country_ind partner partner_ind
	

	reshape wide alpha, i(country country_ind partner year) j(partner_ind) string
	foreach var of varlist alpha* {
		rename `var' `var'_
	}
	reshape wide alpha*, i(country country_ind year) j(partner) string
	order year country country_ind alpha*
	sort country country_ind
	
	compress
	format alpha* %15.0g
	outsheet using "$ALPHAmatlabfolder\WIOD_alpha_`yy'.csv", comma replace
	restore, preserve
}
restore


********************************************************************************
* Weights for sector specific betas - S average across all times 
* - SECOND DEFINITION: S = (sales from sector u to sector s) / (total sales u)
* 					  where s refers to the supplying sector and u to the using sector
********************************************************************************
use "$outputfolder/reduced_WIOD.dta", clear

collapse (sum) inter_use, by(year country_ind partner_ind)

* Generate weights
sort year country_ind partner_ind 
bysort year country_ind: egen tot_sales_usector = total(inter_use)
gen sectorweights_def2 = inter_use/ tot_sales_usector
format sectorweights_def2* %20.0g
bysort year country_ind: egen tot_sectorweights_def2 = total(sectorweights_def2)
tab tot_sectorweights_def2

* reshape to wide and save as csv for matlab use
collapse (mean) sectorweights_def2, by(country_ind partner_ind)

reshape wide sectorweights_def2, i(country_ind) j(partner_ind) string
rename sectorweights_def2* sectorweights_def2_*
compress

outsheet using "$SWEIGHTSmatlabfolder\WIOD_Sweights_alltimeAverage_secondDef.csv", comma replace

* Check row_sum == 1
capture drop rowsum
gen rowsum = sectorweights_def2_15t16 + sectorweights_def2_17t18 + sectorweights_def2_19 + sectorweights_def2_20 + sectorweights_def2_21t22 + sectorweights_def2_23 + /// 
		   sectorweights_def2_24 + sectorweights_def2_25 + sectorweights_def2_26 + sectorweights_def2_27t28 + sectorweights_def2_29 + sectorweights_def2_30t33 + ///
		   sectorweights_def2_34t35 + sectorweights_def2_36t37 + sectorweights_def2_atb + sectorweights_def2_c + sectorweights_def2_e
tab rowsum

********************************************************************************
* Total output weights 
* 17 X 31
********************************************************************************
use "$outputfolder/reduced_WIOD.dta", clear

collapse (firstnm) total_output, by(year country country_ind)
sort year country country_ind

bysort year country: egen tot_total_output = total(total_output)
gen totaloutputweights = total_output/ tot_total_output
bysort year country: egen tot_toweights = total(totaloutputweights)
tab tot_toweights

* reshape to wide and save as csv for matlab use
keep year country country_ind totaloutputweights
preserve

forval yy = $YEAR_START / $YEAR_END {
	drop if year != `yy'	
	compress
	format totaloutputweights* %15.0g
	outsheet using "$TOWEIGHTSmatlabfolder\WIOD_TOweights_`yy'.csv", comma replace
	restore, preserve
}
restore

********************************************************************************
* Total output 
* 17 X 31
********************************************************************************
use "$outputfolder/reduced_WIOD.dta", clear

collapse (firstnm) total_output, by(year country country_ind)
sort year country country_ind

* reshape to wide and save as csv for matlab use
keep year country country_ind total_output
preserve

forval yy = $YEAR_START / $YEAR_END {
	drop if year != `yy'	
	compress
	format total_output* %15.0g
	outsheet using "$totalOutputmatlabfolder\WIOD_totalOutput_`yy'.csv", comma replace
	restore, preserve
}
restore

********************************************************************************
* 18 sectors X 31 countries
********************************************************************************

********************************************************************************
* Generate Srest ROW - 18 X 31
********************************************************************************
use "$outputfolder\WIOD_ordered.dta", clear
* Final sector list: 17 + Srest
local Li_Sectors "15t16 17t18 19 20 21t22 23 24 25 26 27t28 29 30t33 34t35 36t37 atb c e Srest"
* Final country list: 30 + ROW
local Li_Ctrys "AUS	AUT BEL BGR CAN CHN CZE DEU DNK ESP FIN FRA GBR GRC HUN IRL ITA JPN KOR LTU MEX NLD POL PRT ROM RUS SVN SWE TWN USA"

** creating Srest and ROW within WIOD ******************************************
gen tmpd_partner_row = 1
gen tmpd_ctry_row = 1
foreach b of local Li_Ctrys { 
	replace tmpd_ctry_row = 0 if country == "`b'"
	replace tmpd_partner_row = 0 if partner == "`b'"
}
gen tmpd_ctry_Srst = 1
gen tmpd_partner_Srst = 1
foreach b of local Li_Sectors { 
	replace tmpd_ctry_Srst = 0 if country_ind == "`b'"
	replace tmpd_partner_Srst = 0 if partner_ind == "`b'" 
} 
* All remaining to ROW - partnerwise
replace partner = "ROW" if tmpd_partner_row == 1
replace c_E = 999 if tmpd_partner_row == 1

* All remaining to Srest 
replace partner_ind = "Srest" if tmpd_partner_Srst == 1
replace s_E = 999 if tmpd_partner_Srst == 1
*drop if tmpd_partner_Srst == 1

keep inter_use total_output tot_inter_cons tot_val_add  tmpd_ctry_row tmpd_ctry_Srst year c_I s_I c_E s_E country country_ind partner partner_ind
collapse (sum) inter_use (firstnm) total_output tot_inter_cons tot_val_add (mean) tmpd_ctry_row tmpd_ctry_Srst, by(year c_I s_I c_E s_E country country_ind partner partner_ind)

* All remaining to ROW - countrywise
replace country = "ROW" if tmpd_ctry_row == 1
replace c_I = 999 if tmpd_ctry_row == 1

* All remaining to Srest 
replace country_ind = "Srest" if tmpd_ctry_Srst == 1
replace s_I = 999 if tmpd_ctry_Srst == 1
*drop if tmpd_ctry_Srst == 1

keep inter_use total_output tot_inter_cons tot_val_add  c_I s_I c_E s_E year country country_ind partner partner_ind
collapse (sum) inter_use total_output tot_inter_cons tot_val_add (firstnm) c_I s_I c_E s_E, by(year country country_ind partner partner_ind)

tabulate country_ind partner_ind
assert r(r) == r(c)
if r(r) != r(c) {
	break
	display as error "WIOD is not symmetric w.r.t. to Sectors"
}

save "$outputfolder/reduced_WIOD_31cty18sec.dta", replace

********************************************************************************
* GAMMA: share of expenditure on input es by cu in the value of total output_cu 
* 18 X 31
********************************************************************************	
** calculating market shares and scale gammas **********************************
use "$outputfolder/reduced_WIOD_31cty18sec.dta", clear
gen market_share = inter_use/ total_output
replace market_share = 0 if total_output == 0 & inter_use == 0
bysort year country country_ind: egen tot_ms = total(market_share)
gen market_share_rescal = market_share/tot_ms
bysort year country country_ind: egen rescal_tot_ms = total(market_share_rescal)

* generate Linkages in size(NM,NM) weights_cI_sI_cE_sE = Input_cE_sE / Output_cI_sI
keep year c_* s_* countr* partne* market_share market_share_rescal total_output
sort year country country_ind

* reshape to wide and save as csv for matlab use
keep year country country_ind partner partner_ind market_share
preserve

forval yy = $YEAR_START / $YEAR_END {
	drop if year != `yy'	
	sort country country_ind partner partner_ind
	

	reshape wide market_share, i(country country_ind partner year) j(partner_ind) string
	foreach var of varlist market_share* {
		rename `var' `var'_
	}
	reshape wide market_share*, i(country country_ind year) j(partner) string
	order year country country_ind market_share*
	sort country country_ind
	
	compress
	format market_share* %15.0g
	outsheet using "$GAMMAmatlabfolder\WIOD_gamma_31cty18sec_`yy'.csv", comma replace
	restore, preserve
}
restore

********************************************************************************
* ALPHA = sales of products from cu in market rs / total sales of products from cu in all markets 
* 18 X 31
********************************************************************************
** calculating alpha_rscu = Y_rscu / sum(Y_r's'cu) *****************************
use "$outputfolder/reduced_WIOD_31cty18sec.dta", clear

* Y_rscu: sales of products from cu in market rs
bysort year partner partner_ind: egen tot_sales = total(inter_use)
gen alpha = inter_use/ tot_sales
replace alpha = 0 if tot_sales == 0 & inter_use == 0

bysort year partner partner_ind: egen tot_sales_share = total(alpha)
tab tot_sales_share

* reshape to wide and save as csv for matlab use
keep year country country_ind partner partner_ind alpha
preserve

forval yy = $YEAR_START / $YEAR_END {
	drop if year != `yy'	
	sort country country_ind partner partner_ind
	

	reshape wide alpha, i(country country_ind partner year) j(partner_ind) string
	foreach var of varlist alpha* {
		rename `var' `var'_
	}
	reshape wide alpha*, i(country country_ind year) j(partner) string
	order year country country_ind alpha*
	sort country country_ind
	
	compress
	format alpha* %15.0g
	outsheet using "$ALPHAmatlabfolder\WIOD_alpha_31cty18sec_`yy'.csv", comma replace
	restore, preserve
}
restore

********************************************************************************
* Total output weights
* 18 X 31 
********************************************************************************
use "$outputfolder/reduced_WIOD_31cty18sec.dta", clear

collapse (firstnm) total_output, by(year country country_ind)
sort year country country_ind

bysort year country: egen tot_total_output = total(total_output)
gen totaloutputweights = total_output/ tot_total_output
bysort year country: egen tot_toweights = total(totaloutputweights)
tab tot_toweights

* reshape to wide and save as csv for matlab use
keep year country country_ind totaloutputweights
preserve

forval yy = $YEAR_START / $YEAR_END {
	drop if year != `yy'	
	compress
	format totaloutputweights* %15.0g
	outsheet using "$TOWEIGHTSmatlabfolder\WIOD_TOweights_31cty18sec_`yy'.csv", comma replace
	restore, preserve
}
restore


********************************************************************************
* HIGHER ORDER TERMS
********************************************************************************
use "$outputfolder\WIOD_ordered.dta", clear
* Final sector list: all (observed and unobserved)
* Final country list: 30 + ROW
local Li_Ctrys "AUS	AUT BEL BGR CAN CHN CZE DEU DNK ESP FIN FRA GBR GRC HUN IRL ITA JPN KOR LTU MEX NLD POL PRT ROM RUS SVN SWE TWN USA"

** creating ROW within WIOD ****************************************************
gen tmpd_partner_row = 1
gen tmpd_ctry_row = 1
foreach b of local Li_Ctrys { 
	replace tmpd_ctry_row = 0 if country == "`b'"
	replace tmpd_partner_row = 0 if partner == "`b'"
}

* All remaining to ROW - partnerwise
replace partner = "ROW" if tmpd_partner_row == 1
replace c_E = 999 if tmpd_partner_row == 1

keep inter_use total_output tot_inter_cons tot_val_add  tmpd_ctry_row year c_I s_I c_E s_E country country_ind partner partner_ind
collapse (sum) inter_use (firstnm) total_output tot_inter_cons tot_val_add (mean) tmpd_ctry_row, by(year c_I s_I c_E s_E country country_ind partner partner_ind)

* All remaining to ROW - countrywise
replace country = "ROW" if tmpd_ctry_row == 1
replace c_I = 999 if tmpd_ctry_row == 1

keep inter_use total_output tot_inter_cons tot_val_add  c_I s_I c_E s_E year country country_ind partner partner_ind
collapse (sum) inter_use total_output tot_inter_cons tot_val_add (firstnm) c_I s_I c_E s_E, by(year country country_ind partner partner_ind)

tabulate country_ind partner_ind
assert r(r) == r(c)
if r(r) != r(c) {
	break
	display as error "WIOD is not symmetric w.r.t. to Sectors"
}

save "$outputfolder/reduced_WIOD_31cty35sec.dta", replace



********************************************************************************
* GAMMA: share of expenditure on input es by cu in the value of total output_cu 
* 35 X 31 - observed & non-observed
********************************************************************************	
** calculating market shares and scale gammas **********************************
use "$outputfolder/reduced_WIOD_31cty35sec.dta", clear
gen market_share = inter_use/ total_output
replace market_share = 0 if total_output == 0 & inter_use == 0
bysort year country country_ind: egen tot_ms = total(market_share)
gen market_share_rescal = market_share/tot_ms
bysort year country country_ind: egen rescal_tot_ms = total(market_share_rescal)

* Select observed and unobserved sectors
local Li_Sectors_obs "15t16 17t18 19 20 21t22 23 24 25 26 27t28 29 30t33 34t35 36t37 atb c e"
local Li_Sectors_nonobs "50 51 52 h 60 61 62 63 64 j 70 71t74 l m n o p f"

gen tmpd_ctry_obs = 1
gen tmpd_partner_obs = 1
foreach b of local Li_Sectors_nonobs { 
	replace tmpd_ctry_obs = 0 if country_ind == "`b'"
	replace tmpd_partner_obs = 0 if partner_ind == "`b'" 
} 

keep year country country_ind partner partner_ind market_share tmpd_ctry_obs tmpd_partner_obs
sort country country_ind partner partner_ind
save "$GAMMAfolder/temp_31cty35sec.dta", replace

********************************************************************************
* Reshape to wide and save as csv for matlab use 
********************************************************************************
* WHOLE GAMMA matrix
use "$GAMMAfolder/temp_31cty35sec.dta", clear
keep year country country_ind partner partner_ind market_share
preserve
forval yy = $YEAR_START / $YEAR_END {
	drop if year != `yy'	
	sort country country_ind partner partner_ind

	reshape wide market_share, i(country country_ind partner year) j(partner_ind) string
	foreach var of varlist market_share* {
		rename `var' `var'_
	}
	reshape wide market_share*, i(country country_ind year) j(partner) string
	order year country country_ind market_share*
	sort country country_ind
	
	compress
	format market_share* %15.0g
	outsheet using "$GAMMAmatlabfolder\WIOD_gamma_31cty35sec_`yy'.csv", comma replace
	restore, preserve
}
restore

* GAMMA_OO
use "$GAMMAfolder/temp_31cty35sec.dta", clear
keep if tmpd_ctry_obs == 1 & tmpd_partner_obs == 1
preserve
forval yy = $YEAR_START / $YEAR_END {
	drop if year != `yy'	
	sort country country_ind partner partner_ind

	reshape wide market_share, i(country country_ind partner year) j(partner_ind) string
	foreach var of varlist market_share* {
		rename `var' `var'_
	}
	reshape wide market_share*, i(country country_ind year) j(partner) string
	order year country country_ind market_share*
	sort country country_ind
	
	compress
	format market_share* %15.0g
	outsheet using "$GAMMAmatlabfolder\WIOD_gamma_31cty35sec_`yy'_OO.csv", comma replace
	restore, preserve
}
restore

* GAMMA_ON
use "$GAMMAfolder/temp_31cty35sec.dta", clear
keep if tmpd_ctry_obs == 1 & tmpd_partner_obs == 0
keep year country country_ind partner partner_ind market_share
preserve
forval yy = $YEAR_START / $YEAR_END {
	drop if year != `yy'	
	sort country country_ind partner partner_ind

	reshape wide market_share, i(country country_ind partner year) j(partner_ind) string
	foreach var of varlist market_share* {
		rename `var' `var'_
	}
	reshape wide market_share*, i(country country_ind year) j(partner) string
	order year country country_ind market_share*
	sort country country_ind
	
	compress
	format market_share* %15.0g
	outsheet using "$GAMMAmatlabfolder\WIOD_gamma_31cty35sec_`yy'_ON.csv", comma replace
	restore, preserve
}
restore

* GAMMA_NO
use "$GAMMAfolder/temp_31cty35sec.dta", clear
keep if tmpd_ctry_obs == 0 & tmpd_partner_obs == 1
keep year country country_ind partner partner_ind market_share
preserve
forval yy = $YEAR_START / $YEAR_END {
	drop if year != `yy'	
	sort country country_ind partner partner_ind

	reshape wide market_share, i(country country_ind partner year) j(partner_ind) string
	foreach var of varlist market_share* {
		rename `var' `var'_
	}
	reshape wide market_share*, i(country country_ind year) j(partner) string
	order year country country_ind market_share*
	sort country country_ind
	
	compress
	format market_share* %15.0g
	outsheet using "$GAMMAmatlabfolder\WIOD_gamma_31cty35sec_`yy'_NO.csv", comma replace
	restore, preserve
}
restore

* GAMMA_NN
use "$GAMMAfolder/temp_31cty35sec.dta", clear
keep if tmpd_ctry_obs == 0 & tmpd_partner_obs == 0
keep year country country_ind partner partner_ind market_share
preserve
forval yy = $YEAR_START / $YEAR_END {
	drop if year != `yy'	
	sort country country_ind partner partner_ind
	

	reshape wide market_share, i(country country_ind partner year) j(partner_ind) string
	foreach var of varlist market_share* {
		rename `var' `var'_
	}
	reshape wide market_share*, i(country country_ind year) j(partner) string
	order year country country_ind market_share*
	sort country country_ind
	
	compress
	format market_share* %15.0g
	outsheet using "$GAMMAmatlabfolder\WIOD_gamma_31cty35sec_`yy'_NN.csv", comma replace
	restore, preserve
}
restore

********************************************************************************
* Total output weights
* 35 X 31 
********************************************************************************
use "$outputfolder/reduced_WIOD_31cty35sec.dta", clear

collapse (firstnm) total_output, by(year country country_ind)
sort year country country_ind

bysort year country: egen tot_total_output = total(total_output)
gen totaloutputweights = total_output/ tot_total_output
bysort year country: egen tot_toweights = total(totaloutputweights)
tab tot_toweights

* Reshape to wide and save as csv for matlab use
keep year country country_ind totaloutputweights
preserve

forval yy = $YEAR_START / $YEAR_END {
	drop if year != `yy'	
	compress
	format totaloutputweights* %15.0g
	outsheet using "$TOWEIGHTSmatlabfolder\WIOD_TOweights_31cty35sec_`yy'.csv", comma replace
	restore, preserve
}
restore

